package tsing.javase.javaweb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author Tsing/吴清平
 * @Date 2022/10/12 9:34
 * @Version 1.0
 * JSP中的过滤器
 *
 */

@WebFilter("/file/") //设置需要拦截的请求
public class FilterDemo  implements Filter {

    //初始化
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    //拦截器的逻辑
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入拦截器");
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response =(HttpServletResponse) servletResponse;
        HttpSession session = request.getSession(); //获取session
        filterChain.doFilter(servletRequest,servletResponse);//必须放行，进入下一个拦截器or 访问目标资源
    }

    //销毁
    @Override
    public void destroy() {

    }
}
